package com.sojpt.boot;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.jfinal.core.JFinal;
import com.jfinal.core.JFinalFilter;

@Component
public class SoJptBoot {

	@Autowired
	private ServletContext servletContext;

	@Autowired
	private SoJptConfig soJptBootConfig;

	public static JFinalFilter jFinalFilter;
	
	public static SoJptBootFilterChain soJptBootFilterChain = new SoJptBootFilterChain();

	@Bean
	private void start() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException {

		if (null != JFinal.me().getServletContext()) return; //避免SpringBoot热加载重新启动arp造成异常
		jFinalFilter = new JFinalFilter(soJptBootConfig);
		SoJptFilterConfig soJptFilterConfig = new SoJptFilterConfig(servletContext);
		try {
			jFinalFilter.init(soJptFilterConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Bean
	public ExitCodeGenerator exitCodeGenerator() {
		return () -> {
			jFinalFilter.destroy();
			return 42;
		};
	}
	

}
